package com.yingwei.mining.view;

// import java.awt.Color;
import java.awt.Graphics;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yingwei.mining.control.Constant;
import com.yingwei.mining.model.businessobject.UnloadArea;
import com.yingwei.mining.model.businessobject.WorkOrder;

public class UnloadAreaStatusPanel extends MapDeskTopPane
{
    private static final long serialVersionUID = -6906854448381341709L;
    private static Logger logger = LoggerFactory
	    .getLogger(UnloadAreaStatusPanel.class);
    Thread relaxer;
    private static boolean showId = false;

    public UnloadAreaStatusPanel()
    {
    }

    protected void paintComponent(Graphics g)
    {
	paintWorkOrders(g);
    }

    public void paintWorkOrders(Graphics g)
    {
	int topLeftX = 10;
	int topLeftY = 60;
	int rowCnt = 0;
	int colCnt = 0;
	int rowHeight = 50;
	int colWidth = 65;
	int trucksPerLine = 15;

	drawLegend(g);
	g.setColor(Constant.TEXT_COLOR);
	for (UnloadArea ua : data.getUnloadAreas())
	{
	    if (!ua.getStatus().getStatusId().equalsIgnoreCase(
		    Constant.UNLOAD_AREA_STATUS_INACTIVE))
	    {
		if (ua.getStatus().getStatusId().equalsIgnoreCase(
			Constant.UNLOAD_AREA_STATUS_BUSY))
		{
		    g.setColor(Constant.COLOR_UNLOAD);
		}
		else
		{
		    g.setColor(Constant.COLOR_WAIT_UNLOAD);
		}
		g.drawImage(excavatorImage, topLeftX, topLeftY + rowCnt
			* rowHeight, this);
		g.drawString((showId ? (ua.getId() + ":") : "") + ua.getName(),
			topLeftX, topLeftY + rowCnt * rowHeight + 35);
		colCnt = 0;
		for (WorkOrder wo : data.getWorkOrders())
		{
		    if (ua.getId() == wo.getUnloadArea().getId()
			    && wo.getStatus().equalsIgnoreCase(
				    Constant.WORK_ORDER_STATUS_SCHEDULED)
			    && (wo.getTruck().getStatus().getStatusId()
				    .equalsIgnoreCase(
					    Constant.TRUCK_STATUS_WAIT_UNLOAD)
				    || wo
					    .getTruck()
					    .getStatus()
					    .getStatusId()
					    .equalsIgnoreCase(
						    Constant.TRUCK_STATUS_UNLOAD) || wo
				    .getTruck()
				    .getStatus()
				    .getStatusId()
				    .equalsIgnoreCase(
					    Constant.TRUCK_STATUS_MOVE_TO_UNLOAD)))
		    {
			if (wo.getTruck().getStatus().getStatusId()
				.equalsIgnoreCase(Constant.TRUCK_STATUS_UNLOAD))
			{
			    g.setColor(Constant.COLOR_UNLOAD);
			}
			else if (wo.getTruck().getStatus().getStatusId()
				.equalsIgnoreCase(
					Constant.TRUCK_STATUS_WAIT_UNLOAD))
			{
			    g.setColor(Constant.COLOR_WAIT_UNLOAD);
			}
			else if (wo.getTruck().getStatus().getStatusId()
				.equalsIgnoreCase(
					Constant.TRUCK_STATUS_MOVE_TO_UNLOAD))
				{
				    g.setColor(Constant.COLOR_MOVE_TO_UNLOAD);
				}
			else
			{
			    g.setColor(Constant.COLOR_INACTIVE);
			}
			if (colCnt > trucksPerLine)
			{
			    colCnt = 0;
			    rowCnt++;
			}
			g
				.drawImage(truckImage, topLeftX + 60 + colCnt
					* colWidth, topLeftY + rowCnt
					* rowHeight, this);
			g.drawString((showId ? (wo.getTruck().getId() + ":")
				: "")
				+ wo.getTruck().getName(), topLeftX + 60
				+ colCnt * colWidth, topLeftY + rowCnt
				* rowHeight + 35);
			colCnt++;
		    }
		}
		rowCnt++;
	    }
	}
	for (UnloadArea ua : data.getUnloadAreas())
	{
	    if (ua.getStatus().getStatusId().equalsIgnoreCase(
		    Constant.UNLOAD_AREA_STATUS_INACTIVE))
	    {
		g.drawImage(excavatorImage, topLeftX, topLeftY + rowCnt
			* rowHeight, this);
		g.setColor(Constant.COLOR_INACTIVE);
		g.drawString((showId ? (ua.getId() + ":") : "") + ua.getName(),
			topLeftX, topLeftY + rowCnt * rowHeight + 35);
		rowCnt++;
	    }
	}
    }
}
